package com.ypd.shop.service.impl;

import java.util.List;

import com.ypd.common.constant.AuctionConstants;
import com.ypd.common.utils.SecurityUtils;
import com.ypd.shop.domain.AuctionItem;
import com.ypd.shop.service.IAuctionItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ypd.shop.mapper.PostBoxMapper;
import com.ypd.shop.domain.PostBox;
import com.ypd.shop.service.IPostBoxService;

/**
 * 邮箱Service业务层处理
 * 
 * @author lin
 * @date 2025-02-22
 */
@Service
public class PostBoxServiceImpl implements IPostBoxService 
{
    @Autowired
    private PostBoxMapper postBoxMapper;
    @Autowired
    private IAuctionItemService auctionItemService;

    /**
     * 查询邮箱
     * 
     * @param id 邮箱主键
     * @return 邮箱
     */
    @Override
    public PostBox selectPostBoxById(Long id)
    {
        PostBox postBox = postBoxMapper.selectPostBoxById(id);
        postBox.setStatus(true);
        updatePostBox(postBox);
        return postBox;
    }

    /**
     * 查询邮箱列表
     * 
     * @param postBox 邮箱
     * @return 邮箱
     */
    @Override
    public List<PostBox> selectPostBoxList(PostBox postBox)
    {
        postBox.setRecipientId(SecurityUtils.getUserId());
        return postBoxMapper.selectPostBoxList(postBox);
    }

    /**
     * 新增邮箱.每次进行新增的时候直接记录下来发送邮件人的姓名,然后节省后续得多表查询
     * 
     * @param postBox 邮箱
     * @return 结果
     */
    @Override
    public int insertPostBox(PostBox postBox)
    {
        postBox.setSenderId(SecurityUtils.getUserId());
        postBox.setSenderName(SecurityUtils.getUsername());
        return postBoxMapper.insertPostBox(postBox);
    }

    /**
     * 发送邮件提醒用户
     *
     * @param itemId , userId,status
     */
    public void sendMail(String itemId, List<String> userIds,Integer event) {
        String content = null;
        //根据id查询item
        AuctionItem item = auctionItemService.selectAuctionItemByItemId(itemId);
        String name = item.getItemName();
        if (event == AuctionConstants.EVENT_START_NUM){
            content = "您报名的【"+itemId+": "+name+"】"+AuctionConstants.EVENT_START+"，点击拍卖记录查看！";
        }else if (event == AuctionConstants.EVENT_END_NUM){
            content = "您报名的【"+itemId+": "+name+"】"+AuctionConstants.EVENT_END+"，点击拍卖记录查看！";
        }else if (event == AuctionConstants.EVENT_RERUN_NUM){
            content = "您报名的【"+itemId+": "+name+"】"+AuctionConstants.EVENT_RERUN+"，点击拍卖记录查看！";
        }else if (event == AuctionConstants.EVENT_EXCEED_NUM){
            content = "您在【"+itemId+": "+name+"】"+AuctionConstants.EVENT_EXCEED+"，点击拍卖记录查看！";
        }
        for (int i = 0; i < userIds.size(); i++) {
            postBoxMapper.insertPostBox(PostBox.builder().senderId(AuctionConstants.ID_RUOYI).senderName(AuctionConstants.USERNAME_RUOYI)
                    .recipientId(Long.valueOf(userIds.get(i)))
                    .content(content).build());
        }
    }

    /**
     * 修改邮箱
     * 
     * @param postBox 邮箱
     * @return 结果
     */
    @Override
    public int updatePostBox(PostBox postBox)
    {
        return postBoxMapper.updatePostBox(postBox);
    }

    /**
     * 批量删除邮箱
     * 
     * @param ids 需要删除的邮箱主键
     * @return 结果
     */
    @Override
    public int deletePostBoxByIds(Long[] ids)
    {
        return postBoxMapper.deletePostBoxByIds(ids);
    }

    /**
     * 删除邮箱信息
     * 
     * @param id 邮箱主键
     * @return 结果
     */
    @Override
    public int deletePostBoxById(Long id)
    {
        return postBoxMapper.deletePostBoxById(id);
    }
}
